<?php 
//创建socket服务
$c=stream_socket_server("tcp://127.0.0.1:1100");
while(1)
{
  if(is_resource($a=stream_socket_accept($c)))
  {
    //do something time-consuming
    //获取网络socket名称
    if(stream_socket_get_name($a,true)!==false)
    {
      fwrite($a,"regards form berlin");
    }
  }
}

//

//创建两个无区别的socket数据流连接
$sockets=stream_socket_pair(stream_pf_unix,stream_sock_stream,stream_ipproto_ip);
//创建子进程并返回其pid
$pid=pcntl_fork();
if($pid==-1)
{
  //如果没有子进程
  die('could not fork');
}
else if($pid)
{
 
  fclose($sockets[0]);
  fwrite($sockets[1], "child pid: $pidn");
  echo fgets($sockets[1]);
  fclose($sockets[1]);
}
else
{
 
  fclose($sockets[1]);
  fwrite($sockets[0], "message from childn");
  echo fgets($sockets[0]);
  fclose($sockets[0]);
}

//


$server=stream_socket_server('tcp://127.0.0.1:1234');

$socket=stream_socket_accept($server);

echo "received out-of-band:'".stream_socket_recvfrom($socket,1500,stream_oob)."'n";

echo "data:'".stream_socket_recvfrom($socket,1500,stream_peek)."'n";

echo "data:'".stream_socket_recvfrom($socket,1500)."'n";

fclose($socket);
fclose($server);

//


$socket=stream_socket_client('tcp://127.0.0.1:1234');

fwrite($socket,"normal data transmit.");

stream_socket_sendto($socket,"out of band data.",stream_oob);

fclose($socket);

//

//创建服务端
$socket=stream_socket_server("tcp://0.0.0.0:8000",$errno,$errstr);
//如果创建失败
if(!$socket)
{
  echo "$errstr ($errno)<br />n";
}
//如果创建成功
else
{
  //接受连接
  while($conn=stream_socket_accept($socket))
  {
    //写入数据
    fwrite($conn,'the local time is '.date('n/j/y g:i a')."n");
    //关闭连接
    fclose($conn);
  }
  //关闭socket
  fclose($socket);
}